package com.ouyang.dao.User;

import com.ouyang.dao.DataBaseDao;
import com.ouyang.poji.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDaoimpl implements UserDao {
    @Override
    public User getloginuser ( Connection connection, String UserName ) throws Exception {
        PreparedStatement pstm = null;
        ResultSet rs = null;
        User user = null;
        if (connection != null) {
            String sql = "select * from User where UserName=?";
            Object[] params = {UserName};
            rs = DataBaseDao.executers(connection, pstm, rs, sql, params);
            if (rs.next()) {
                user = new User();
                user.setUsername(rs.getString("UserName"));
                user.setUserpassword(rs.getString("Password"));
                user.setEmail(rs.getString("Email"));
                System.out.println("查询成功");
            }
            DataBaseDao.closeResource(null, pstm, rs);
        }
        return user;
    }

    @Override
    public boolean changepwd ( String UserName, String pwd ) {
        Connection connection = DataBaseDao.getConnection();
        PreparedStatement statement = null;
        String sql = "update User set PassWord = ? where UserName = ?";
        try {
            statement = connection.prepareStatement(sql);
            statement.setString(1, pwd);
            statement.setString(2, UserName);
            statement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DataBaseDao.closeResource(connection, statement, null);
        }
        return true;
    }

    @Override
    public List<User> getuserlist ( ) {
        Connection connection = DataBaseDao.getConnection();
        PreparedStatement statement = null;
        String sql = "select * from User";
        ResultSet rs = null;
        List<User> list = new ArrayList<>();
        try {
            statement = connection.prepareStatement(sql);
            rs = statement.executeQuery();
            while (rs.next()){
                User user = new User();
                user.setUserid(rs.getInt(1));
                user.setUsername(rs.getString(2));
                user.setUserpassword(rs.getString(3));
                user.setEmail(rs.getString(4));
                list.add(user);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DataBaseDao.closeResource(connection,statement,rs);
        }

        return list;
    }

    @Override
    public List<User> search (String key,String value) {
        Connection connection = DataBaseDao.getConnection();
        PreparedStatement statement = null;
        String sql = "select * from User where "+key+" like '%"+value+"%'";
        ResultSet rs = null;
        List<User> list = new ArrayList<>();
        try {
            statement = connection.prepareStatement(sql);
            rs = statement.executeQuery();
            while (rs.next()){
                User user = new User();
                user.setUserid(rs.getInt(1));
                user.setUsername(rs.getString(2));
                user.setUserpassword(rs.getString(3));
                user.setEmail(rs.getString(4));
                list.add(user);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DataBaseDao.closeResource(connection,statement,rs);
        }

        return list;
    }

    @Override
    public Integer add ( String Username, String Password, String Email ) {
        Connection connection = DataBaseDao.getConnection();
        PreparedStatement statement = null;
        String sql = "insert into User(UserName,PassWord,Email) values(?,?,?)";
        Integer rs = null;
        try {
            statement = connection.prepareStatement(sql);
            statement.setString(1,Username);
            statement.setString(2,Password);
            statement.setString(3,Email);
            rs = statement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DataBaseDao.closeResource(connection,statement,null);
        }
        return rs;
    }

    @Override
    public Integer updata ( Integer id, String Username, String Password, String Email ) {
        Connection connection = DataBaseDao.getConnection();
        PreparedStatement statement = null;
        String sql = "update User set UserName = ?,PassWord = ?,Email = ? where id = ?";
        Integer rs = null;
        try {
            statement = connection.prepareStatement(sql);
            statement.setString(1,Username);
            statement.setString(2,Password);
            statement.setString(3,Email);
            statement.setString(4, String.valueOf(id));
            rs = statement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DataBaseDao.closeResource(connection,statement,null);
        }

        return rs;
    }

    @Override
    public Integer del ( String id ) {
        Connection connection = DataBaseDao.getConnection();
        PreparedStatement statement = null;
        String sql = "delete from User where Id = "+id;
        Integer rs = null;
        try {
            statement = connection.prepareStatement(sql);
            rs = statement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DataBaseDao.closeResource(connection,statement,null);
        }
        return rs;
    }
}
